package com.example.demo.config;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.util.ArguemntNovalidException;

@ControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(Exception.class)
//	@ResponseBody
	public void exceptionHandler(Exception e, PrintWriter w) {
		e.printStackTrace(w);
//		return "我处理所有的Exception"+e.getMessage();
	}
	
	
	@ExceptionHandler(NullPointerException.class)
//	@ResponseBody
	public void nullPointHandler(Exception e,PrintWriter w) {
		e.printStackTrace(w);
//		return "我处理所有的NullPointerException"+e.getMessage();
	}
	
	@ExceptionHandler(ArguemntNovalidException.class)
//	@ResponseBody
	public ModelAndView argValidHandler(Exception e) {
		Map<String, Object> model = new HashMap<>();
		model.put("msg",e.getMessage());
		return	new ModelAndView("argnotvalid", model);
//		return "我处理所有的NullPointerException"+e.getMessage();
	}
	
//	org.springframework.validation.BindException
}
